<?php

class BugController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function createAction()
    {

        $frmBugReport = new Form_BugReportForm();
        $frmBugReport->setAction('/zf_cms/public/index.php/bug/submit');
        $frmBugReport->setMethod('post');
        $this->view->form = $frmBugReport;
    }

    public function submitAction()
    {
        $bugReportForm = new Form_BugReportForm();
        $bugReportForm->setAction('/zf_cms/public/index.php/bug/submit');
        $bugReportForm->setMethod('post');
        if ($this->getRequest()->isPost()) {
            if ($bugReportForm->isValid($_POST)) {
                $bugModel = new Model_Bug();
// if the form is valid then create the new bug
                $result = $bugModel->createBug(
                        $bugReportForm->getValue('author'), $bugReportForm->getValue('email'), $bugReportForm->getValue('date'), $bugReportForm->getValue('url'), $bugReportForm->getValue('description'), $bugReportForm->getValue('priority'), $bugReportForm->getValue('status')
                );
// if the createBug method returns a result
// then the bug was successfully created

                if ($result) {
                    $this->_forward('confirm');
                }
            }
        }
        $this->view->form = $bugReportForm;
    }

    public function confirmAction()
    {
        // action body
    }

    public function listAction()
    {
        $bugModel = new Model_Bug();
        $this->view->bugs = $bugModel->fetchBugs();
    }

    public function editAction()
    {
        $bugModel = new Model_Bug();
        $bugReportForm = new Form_BugReportForm();
        $bugReportForm->setAction('/zf_cms/public/index.php/bug/edit');
        $bugReportForm->setMethod('post');
        if ($this->getRequest()->isPost()) {
            if ($bugReportForm->isValid($_POST)) {
                $bugModel = new Model_Bug();
// if the form is valid then update the bug
                $result = $bugModel->updateBug(
                        $bugReportForm->getValue('id'), $bugReportForm->getValue('author'), $bugReportForm->getValue('email'), $bugReportForm->getValue('date'), $bugReportForm->getValue('url'), $bugReportForm->getValue('description'), $bugReportForm->getValue('priority'), $bugReportForm->getValue('status')
                );
                return $this->_forward('list');
            }
        } else {
            $id = $this->_request->getParam('id');
            $bug = $bugModel->find($id)->current();
            $bugReportForm->populate($bug->toArray());
//format the date field
            $bugReportForm->getElement('date')->setValue(date('m-d-Y', $bug->date));
        }
        $this->view->form = $bugReportForm;
    }

    public function deleteAction()
    {
                $bugModel = new Model_Bug();
        $id = $this->_request->getParam('id');
        $bugModel->deleteBug($id);
        return $this->_forward('list');
    }


}



